package com.zwt.service.impl;

import com.zwt.bean.TUserRedPacksge;
import com.zwt.bean.TredPacksge;
import com.zwt.dao.TUserRedPacksgeMapper;
import com.zwt.dao.TredPacksgeMapper;
import com.zwt.service.UserRedPacketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by zwt on 2018/6/6.
 */
@Service
public class UserRedPacketServiceImpl implements UserRedPacketService {

    @Autowired
    private TUserRedPacksgeMapper tUserRedPacksgeMapper;
    @Autowired
    private TredPacksgeMapper tredPacksgeMapper;

    @Override
    public int grapRedPacket(Integer redPackageId, Integer userId) {
        TredPacksge tredPacksge = tredPacksgeMapper.selectByPrimaryKey(redPackageId);
        if(tredPacksge.getStock()>0){
            int i = tredPacksgeMapper.decreaseRedPacket(redPackageId);
            //插入抢红包表
            TUserRedPacksge tUserRedPacksge = new TUserRedPacksge();
            tUserRedPacksge.setRedPackageId(redPackageId);
            tUserRedPacksge.setUserId(userId);
            tUserRedPacksge.setAmount(tredPacksge.getUnitAmount());
            tUserRedPacksge.setNote("抢红包"+redPackageId);
            int insert = tUserRedPacksgeMapper.insert(tUserRedPacksge);
            return insert;
        }
        return 0;
    }
}
